import { _decorator, Component, Node } from 'cc';
import { Battle } from '../Battle';
import { EnEvent } from '../Common/const/EnEvent';
import { GameAPI } from '../Common/GameAPI';
import { FormationFollowController } from './FormationFollowController';
const { ccclass, property } = _decorator;

@ccclass('TeammateManager')
export class TeammateManager extends Component {

    @property(Node)
    private team1Node: Node = null;
    @property(Node)
    private team2Node: Node = null;
    @property(Node)
    private team3Node: Node = null;
    @property(Node)
    private team4Node: Node = null;
    @property(Node)
    private team5Node: Node = null;
    @property(Node)
    private roleBornNode: Node = null;
    @property(Node)
    private joystickNode: Node = null;

    private curBornNode: Node;
    private teamList: Array<Node> = [];

    start() {
        this.teamList.push(this.team1Node);
        this.teamList.push(this.team1Node);
        this.teamList.push(this.team2Node);
        this.teamList.push(this.team3Node);
        this.teamList.push(this.team4Node);
        this.teamList.push(this.team5Node);
        GameAPI.RegGEvent(EnEvent.ShowTeamEvent, this.OnShowTeamEvent.bind(this), this);
    }

    onDestroy() {
        GameAPI.OffAllCaller(this);
    }

    OnShowTeamEvent(heroIndex) {
        if (this.teamList.length > heroIndex) {
            this.joystickNode.active = false;
            this.roleBornNode.active = true;
            let heroNode = this.teamList[heroIndex];
            heroNode.active = true;
            this.curBornNode = this.roleBornNode.getChildByName(heroNode.name);
            this.curBornNode.active = true;
            let shadow = heroNode.getChildByName("shadow");
            shadow.active = false;
            let body = heroNode.getChildByName("body");
            body.active = false;
            this.scheduleOnce(function () {
                this.joystickNode.active = true;
                this.roleBornNode.active = false;
                this.curBornNode.active = false;
                shadow.active = true;
                body.active = true;
                let teamController: FormationFollowController = this.teamList[heroIndex].getComponent(FormationFollowController);
                teamController.BeginFollower();
            }.bind(this), 2);
        }
    }

}


